package com.punai.gk.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.enums.BusinessType;
import com.punai.gk.domain.CodeLayout;
import com.punai.gk.domain.constants.GkConstants;
import com.punai.gk.service.ICodeLayoutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 编码格式Controller
 * 
 * @author zhx
 * @date 2023-06-08
 */
@RestController
@RequestMapping("/gk/codeLayout")
public class CodeLayoutController extends BaseController
{
    @Autowired
    private ICodeLayoutService codeLayoutService;

    @GetMapping(value = "/{type}/{specId}")
    public AjaxResult getInfo(@PathVariable("type") String type,@PathVariable("specId") String specId)
    {
        if(!StrUtil.containsAny(type, GkConstants.CL_TYPE,GkConstants.JX_TYPE,GkConstants.GX_TYPE,GkConstants.FB_TYPE)){
            AjaxResult.error("参数错误");
        }
        if(StrUtil.containsAny(type,GkConstants.GX_TYPE,GkConstants.FB_TYPE)){
            if(StrUtil.isEmpty(specId) || StrUtil.equals("null",specId)){
                AjaxResult.error("专业获取失败");
            }
        }else {
            specId = "";
        }
        return AjaxResult.success(codeLayoutService.selectCodeLayoutByType(type,specId));
    }

    @PreAuthorize("@ss.hasPermi('gk:codeLayout:editCl')")
    @Log(title = "材料编码格式", businessType = BusinessType.UPDATE)
    @PutMapping("/editCl")
    public AjaxResult editCl(@RequestBody CodeLayout codeLayout)
    {
        checkSuffixLen(codeLayout.getSuffixLen());
        return toAjax(codeLayoutService.updateCodeLayout(codeLayout));
    }

    @PreAuthorize("@ss.hasPermi('gk:codeLayout:editJx')")
    @Log(title = "机械编码格式", businessType = BusinessType.UPDATE)
    @PutMapping("/editJx")
    public AjaxResult editJx(@RequestBody CodeLayout codeLayout)
    {
        checkSuffixLen(codeLayout.getSuffixLen());
        return toAjax(codeLayoutService.updateCodeLayout(codeLayout));
    }


    @PreAuthorize("@ss.hasPermi('gk:codeLayout:editGx')")
    @Log(title = "标准编码格式", businessType = BusinessType.UPDATE)
    @PutMapping("/editGx")
    public AjaxResult editGx(@RequestBody CodeLayout codeLayout)
    {
        checkSuffixLen(codeLayout.getSuffixLen());
        return toAjax(codeLayoutService.updateCodeLayout(codeLayout));
    }

    @PreAuthorize("@ss.hasPermi('gk:codeLayout:editFb')")
    @Log(title = "成本编码格式", businessType = BusinessType.UPDATE)
    @PutMapping("/editFb")
    public AjaxResult editFb(@RequestBody CodeLayout codeLayout)
    {
        checkSuffixLen(codeLayout.getSuffixLen());
        return toAjax(codeLayoutService.updateCodeLayout(codeLayout));
    }

    void checkSuffixLen(Integer suffixLen) {
        if (suffixLen < 1) {
           throw new RuntimeException("编码后缀长度不能小于1");
        }
    }

}
